package com.camunda.workflow.config.saToken;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpLogic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * [Sa-Token 权限认证] 配置类
 *
 * @author kong
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
    /**
     * 权限接口实现(使用bean注入方便用户替换)
     */
    @Bean
    public StpInterface stpInterface() {
        return new StpInterfaceImpl();
    }

    /**
     * 注册 Sa-Token 拦截器打开注解鉴权功能
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor()).excludePathPatterns("/**");
//        registry.addInterceptor(new SaInterceptor(handler -> SaRouter.match("/**")
//                .notMatch("/**/login", "/**/error", "/**/*.css", "/**/*.js", "/**/*.html", "/**/*.ts"
//                        , "/**/*.jpeg", "/**/*.jpg", "/**/*.png", "/**/*.psd", "/**/*.ai", "/**/*.eps", "/**/*.svg", "/**/*.gif"
//                        , "/**/*.ttf", "/**/*.otf", "/**/*.ttc", "/**/*.ico"
//                        , "/**/swagger-ui.html", "/**/doc.html", "/**/webjars/**"
//                        , "/**/swagger-resources/**", "/**/swagger/**", "/**/v2/**", "/**/v3/**"
//                )
////					.notMatch("/**")
//                .check(r -> {
//                    HttpServletResponse response = (HttpServletResponse) SaHolder.getResponse().getSource();
//                    try {
//                        StpUtil.checkLogin();
////							if(!StpUtil.isLogin()){
////								RespUtil.writeResponse(response,AjaxResult.error(501, "未登录"));
////								SaRouter.back();
////							}
//
//                        if (ObjUtil.isEmpty(LoginHelper.getLoginUser())) {
//                            RespWriter.writeResp(response, RestResp.fail(501, "未登录"));
//                            SaRouter.back();
//                        }
////                            Set<String> permissions = new HashSet<>();
////                            // 增加不需要认证的路径
////                            permissions.add("/**/auth/**");
////                            loginUser.getMenus().forEach(e -> {
////                                permissions.add("/**"+e.getRoutePath());
////                            });
////                            System.out.println("---------------: "+SaHolder.getRequest().getRequestPath());
////                            if (!SaRouter.match(new ArrayList<>(permissions)).isHit()) {
////                                RespUtil.writeResponse(response, AjaxResult.error("没有权限"));
////                                SaRouter.back();
////                            }
//                    } catch (Exception e) {
//                        RespWriter.writeResp(response, RestResp.fail(501, "未登录"));
//                        SaRouter.back();
//                    }
//
//                }))).addPathPatterns("/**");
    }

    /**
     * Sa-Token 整合 jwt
     */
    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }

}
